package com.biz.core.config.component.mybatisplus;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * Package com.uni.cashier.back.config.component.mybatisplus<br>
 * @author yangfan <br>
 * Description: TODO<br>
 * date 2019/12/25 5:50 下午<br>
 * version V1.0
 */
@Slf4j
@Configuration
@MapperScan("com.biz.core.mapper.universal")
public class MyBatisPlusConfig {

//    @Value("${db_qndatacenter}")
//    private String database;
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        log.info("Register paging plugin");
        return new PaginationInterceptor();
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        HashMap<String, TableNameHandler> map = new HashMap<String, TableNameHandler>(2) {{
//            put("t_device", (sql, tableName) -> {
//                return database +"."+tableName;
//            });
        }};
        dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        return interceptor;
    }


}
